#include "syscall.h"

//#define SHELL_TEST
#define NULL 0


/* 
 * mystrcmp, compare two strings.
 * return 0, if equal; 1, if str1 > str2; -1, if str1 < str2.
 */
int mystrcmp(const char* str1, const char* str2)
{
    while ((*str1==*str2) && *str1 && *str2)
    {
        str1++;
        str2++;
    }
	if ((*str1==NULL)&&(*str2==NULL))
		return 0;
	else if (*str2==NULL)
		return 1;
	else
		return -1;
//    return (*str1==NULL)&&(*str2==NULL);
}

/* 
 * readArgv, analyze the input commander.
 * devide it to several segments by space.
 */
/*
void readArgv(char **argv)
{
	char cmd[60];
	int i, j, k;
	
	k = 0;
	do {
	Read(&cmd[k], 1, ConsoleInput);
	} while( (cmd[k++] != '\n') );
	cmd[--k] = '\0';
//Print(argv[0]);

	i = 0;
	j = 0;
	k = 0;
	do {
		j = 0;
		do {
			argv[i][j++] = cmd[k++]; 
		}  while( (cmd[k-1] != ' ') && (cmd[k-1] != '\0') );
		argv[i][--j] = '\0';

	} while( argv[i++][j] != '\0' );
}
*/

/*
 * Print the help information.
 */
void myhelp(char *cmd)
{
	Print("the help information!\n");
}

/* 
 * main, process as follows:
 * 1. analyze the input commander;
 * 2. call the relative system call according to the cmd.
 * support the following cmd:
 * 1. touch, create a new file
 * 2. cp, copy the Nachos file1 to file2
 * 3. ap, append the Nachos file1 to the end of file2
 * 4. mv, move the file (or dirctory) form path1 to path2
 * 5. rm, remove the file
 * 6. mkdir, create the file directory
 * 7. rmdir, romove the directory
 * 8. pwd, print the work directory
 * 9. cd, change the work directory
 * 10.ls, list the file and content under the dirctory
 * 11.echo, print the comment on the screen\
 * 12.help, print help information
 * 13.exit, exit the shell
 */
int
main()
{
    SpaceId newProc;
    OpenFileId input = ConsoleInput;
    OpenFileId output = ConsoleOutput;
    char cmd[60];
	char argv[4][20];
    int i,j,k;

	Print("\nWelcome to my shell!\n\n");

    while( 1 )
    {
		Print("seedclass@nachos$ ");

		for (i = 0; i < 4; i++)
		{
			argv[i][0] = '\0';
		}
	
//		readArgv(argv);
		k = 0;	
		do {
			Read(&cmd[k], 1, input); 
		} while( (cmd[k++] != '\n') );
		cmd[--k] = '\0';

#ifdef SHELL_TEST
		Print(cmd);
		Print("\n");
#endif

		i = 0;
		j = 0;
		k = 0;
		do {
			j = 0;
			do {
				argv[i][j++] = cmd[k++];
			} while( (cmd[k-1] != ' ') && (cmd[k-1] != '\0') );
			argv[i++][--j] = '\0';

		} while( cmd[k-1] != '\0' ); 
		
#ifdef SHELL_TEST
		// print the argv for test
		for (i = 0; (i < 4) && (argv[1][0] != '\0'); i++)
		{
			Print(argv[i]);
			Print("\n");
		}
#endif

		if (mystrcmp(argv[0], "touch") == 0)
		{
			Create(argv[1]);
		}
		else if (mystrcmp(argv[0], "cp") == 0)
		{
			//Print("in cp!\n");

			Copy(argv[1], argv[2]);
		}
		else if (mystrcmp(argv[0], "ap") == 0)
		{
			//Print("in ap!\n");

			Append(argv[1], argv[2]);
		}
		else if (mystrcmp(argv[0], "mv") == 0)
		{
			Move(argv[1], argv[2]);
		}
		else if (mystrcmp(argv[0], "rm") == 0)
		{
			Remove(argv[1]);
		}
		else if (mystrcmp(argv[0], "rn") == 0)
		{
			Rename(argv[1], argv[2]);
		}
		else if (mystrcmp(argv[0], "mkdir") == 0)
		{
			Mkdir(argv[1]);
		}
		else if (mystrcmp(argv[0], "rmdir") == 0)
		{
			Rmdir(argv[1]);
		}
		else if (mystrcmp(argv[0], "pwd") == 0)
		{
			Pwd();
		}
		else if (mystrcmp(argv[0], "cd") == 0)
		{
			Changedir(argv[1]);
		}
		else if (mystrcmp(argv[0], "ls") == 0)
		{
			Listdir(argv[1]);
		}
		else if (mystrcmp(argv[0], "--help") == 0)
		{
			myhelp(argv[1]);
		}
		else if (mystrcmp(argv[0], "echo") == 0)
		{
			//Print("in echo!\n");

			Echo(argv[1]);
		}
		else if (mystrcmp(argv[0],"exit") == 0) 
		{
			//Print("in exit!\n");

			Halt(); 
		}

		else if ( argv[0][0] != '\0' ) {
			//Print("in exec!\n");

			newProc = Exec(cmd);
		}
		else
		{		
			;
		}
    }
}

